<?php
require_once ("../src/db/connectdatabase.php");

function getDatas($product, $bank, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "Select i.keyipro, i.prkey, p.prname, i.bankid, b.bankname, b.banklogo, i.datecreate, i.isavalible FROM loan_for_intro_product as i, banks as b, loan_products_rate_for as p WHERE i.prkey=p.prkey AND i.bankid=b.bankid AND i.prkey LIKE '%".$product."%' AND i.bankid LIKE '%".$bank."%' ORDER BY i.datecreate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

function getRowData($product, $bank) {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM loan_for_intro_product WHERE prkey LIKE '%".$product."%' AND bankid LIKE '%".$bank."%'";
	return $con -> getvalueString($sql);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$product = ""; 
if(isset($_GET['key'])) {
	$product = $_GET['key'];
}

$bank = "";
if(isset($_GET['bank'])) {
	$bank = $_GET['bank'];
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRowData($product, $bank);
$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

//load data
$arrdata = getDatas($product, $bank, ($page-1)*$numrecord, $numrecord);
?>
<style>
.listquestionview table {
	width: 850px;
	max-width: 850px;
}

.listquestionview table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.listquestionview table tr:hover td {
	background: #FFC;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.styleobject {
	width: 200px;
}

.stylequestionname {
	width: 250px;
}

.styledatecreatequestion {
	width: 100px;
	max-width: 100px;
	text-align: center;
}

.styleacctionquestion {
	width: 150px;
	text-align: center;
	max-width: 150px;
}

.namequestionline {
	text-align: left;
	padding-left: 5px;
	padding-right: 5px;
}

.listquestionview table td img {
	cursor: pointer;
}

.nameformquestion {
	background: url(../images/bg-login.png);
	-moz-border-radius: 2px 2px 2px 2px;
	-webkit-border-radius: 2px 2px 2px 2px;
	border-radius: 2px 2px 2px 2px;
}

.linequestiondate {
	text-align: center;
}

.stylestt {
	width: 40px;
}

.class-pagination{
		float: right;
		clear: left;
		padding-top: 20px;
	}
	
	.class-pageLabel{
		font-size: 11px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.class-pageNow{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		background-color: #C0C0C0;
		border-radius: 4px;
		box-shadow: 0 0 10px rgba(0,0,0,.1);
		cursor: default;
	}
	
	.class-pageElem{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		box-shadow: 0 0 10px rgba(0,0,0,.2);
		border-radius: 4px;
		background-color: white;
	}

	.class-pageElem:hover{
		padding: 5px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		background-color: #C0C0C0;
		cursor: pointer;
	}
	.submitform {
		background: #004A00;
		text-indent: 1px;
		width:80px;
		height:30px;
		cursor: pointer;
		font-weight: bold;
		font-size: 14px;
	}
</style>

<h3>Thông tin ngân hàng cho vay theo đối tượng</h3>
<div class="form-search">
	<div style="width:600px; float:left;">	
    	<table>
        	<tr>
                <td>Đối tượng</td>
                <td>Ngân hàng</td>
                <td></td>
            </tr>
            <tr>
                <td><select name="selectobject" class="objectloadselect" style="width: 150px; height: 30px; margin:5px; padding:5px;"><option value="">--- Tất Cả ---</option></select></td>
                <td><select name="selectbank" class="bankloadselect" style="width: 150px; height: 30px; margin:5px; padding:5px;"><option value="">--- Tất Cả ---</option></select></td>
                <td><input id="form_submitsearch" type="button" style="cursor:pointer ;background-image:url(../images/timkiem.png); width:70px; height:24px; border:0px;" value="" /></td>
            </tr>
        </table>
    </div>
</div>
<div style="clear:both; margin-top: 10px;"></div>
<div><a href="admin.php?content=quan-ly-doi-tuong-vay&amp;as=them-bank-loan-object"><img src="../images/icon-add.jpg" title="Thêm" />Thêm</a></div>
<div style="clear:both;"></div>
<div class="class-pagination">
    <span class="class-pageLabel">Trang</span>
    <?php if($page == 1) { ?>
    <span title="Trang 1" page="1" class="class-pageNow">1</span>
    <?php } else { ?>
    <span title="Trang 1" page="1" class="class-pageElem">1</span>
    <?php }?>
    <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
    <?php if($i == $page) { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
    <?php } else { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
    <?php }?>
    <?php }}?>
</div>
<div style="clear:both; height:10px;"></div>
<div class="listquestionview">
	<table>
    	<tr>
        	<th class="stylestt title">STT</th>
            <th class="styleobject title">Đối tượng</th>
        	<th class="stylequestionname title">Ngân hàng</th>
            <th class="styledatecreatequestion title">Ngày C.Nhật</th>
            <th class="styleacctionquestion title">Chức năng</th>
        </tr>
  		<?php
		if(count($arrdata) > 0) {
			$i = ($page-1)*$numrecord + 1;
			foreach($arrdata as $entry) {
		?>
        <tr>
        	<td class="nameformquestion" align="center"><?php echo($i) ?></td>
        	<td class="nameformquestion linequestiondate"><?php echo($entry[2]) ?></td>
            <td class="nameformquestion linequestiondate"><?php echo($entry[4]) ?>(<?php echo($entry[3]) ?>)</td>
            <td class="nameformquestion linequestiondate"><?php echo(int_to_date($entry[6])) ?></td>
            <td class="nameformquestion linequestiondate"><?php if($entry[7] == "1") { ?><img class="class-display-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/unclockicon.png" height="20" title="Click để không cho phép hiển thị" /><?php } else { ?><img class="class-hiden-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" height="20" /><?php } ?>&nbsp; | &nbsp;<a href="admin.php?content=quan-ly-doi-tuong-vay&amp;as=update-bank-loan-object&amp;id=<?php echo($entry[0]) ?>"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="<?php echo($entry[0]) ?>" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" /></td>
        </tr>
        <?php
			$i++;
			}
		} else {
			?>
        <tr><th colspan="5">Không tồn tại dữ liệu</th></tr>
            <?php
		}
		?>
        
    </table>
</div>
<div style="clear:both;"></div>
<div class="class-pagination">
    <span class="class-pageLabel">Trang</span>
    <?php if($page == 1) { ?>
    <span title="Trang 1" page="1" class="class-pageNow">1</span>
    <?php } else { ?>
    <span title="Trang 1" page="1" class="class-pageElem">1</span>
    <?php }?>
    <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
    <?php if($i == $page) { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
    <?php } else { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
    <?php }?>
    <?php }}?>
</div>
<div style="clear:both; height:10px;"></div>
<div><a href="admin.php?content=quan-ly-doi-tuong-vay&amp;as=them-bank-loan-object"><img src="../images/icon-add.jpg" title="Thêm" />Thêm</a></div>

<script>
loadbank();
loaddoituong();
function loadbank() {
	$.ajax({
		url: "../src/ajax/ajaxbank.php",
		type: "post",
		data: {act:'getbanks'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + ' (' + entry[0] + ')</option>';
			});
			$('.bankloadselect').append(html);
			$(".bankloadselect option[value='<?php echo($bank) ?>']").attr('selected', 'selected');
		}
	});
}

function loaddoituong() {
	$.ajax({
		url: "../src/ajax/ajaxObjectLoan.php",
		type: "post",
		data: {act:'get'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '</option>';
			});
			$('.objectloadselect').append(html);
			$(".objectloadselect option[value='<?php echo($product) ?>']").attr('selected', 'selected');
		}
	});
}

$(document).on('click', '.class-pageElem', function() {
	page = $(this).attr('page');
	var bank = $('.bankloadselect').val();
	var key = $('.objectloadselect').val();
	window.location = 'admin.php?content=quan-ly-doi-tuong-vay&as=bank-loan-object&bank=' + bank + '&key=' + key + '&page=' + page;
});

$(document).on('click', '#form_submitsearch', function() {
	var bank = $('.bankloadselect').val();
	var key = $('.objectloadselect').val();
	window.location = 'admin.php?content=quan-ly-doi-tuong-vay&as=bank-loan-object&bank=' + bank + '&key=' + key;
});



$(document).on('click', '.class-display-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/AjaxAdminObjectBankLoan.php",
		data:{act: 'edith',type: "0", idpe: id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-hiden-link" height="20" idpr="' + id + '" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" />&nbsp; | &nbsp;<a href="admin.php?content=quan-ly-doi-tuong-vay&as=update-bank-loan-object&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="' + id + '" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" />');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-hiden-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/AjaxAdminObjectBankLoan.php",
		data:{act: 'edith',type: "1", idpe: id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-display-link" height="20" idpr="' + id + '" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" />&nbsp; | &nbsp;<a href="admin.php?content=quan-ly-doi-tuong-vay&as=update-bank-loan-object&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="' + id + '" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" />');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-deletequestion-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	if(confirm('Bạn thực sự muốn xóa?')) {
		$.ajax({
			type: "POST",
			url: "../src/ajax/AjaxAdminObjectBankLoan.php",
			data:{act: 'del', idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					alert("Cập nhật thành công");
					elem.parent().parent().remove();
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	}
});
</script>